package com.aboverock.module.system.validator.constraintvalidators;

import com.aboverock.core.spring.SpringContextHolder;
import com.aboverock.module.system.service.UserService;
import com.aboverock.module.system.utils.UserInfoUtil;
import com.aboverock.module.system.validator.constraints.UniqueLoginName;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * The Class UniqueLoginNameValidator.
 *
 * @author Rock Wang
 */
public class UniqueLoginNameValidator implements ConstraintValidator<UniqueLoginName, String> {

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        UserService userService = SpringContextHolder.getBean(UserService.class);

        String loginName = UserInfoUtil.addSuffix2LocalUserLoginName(value);
        if (null == userService.getLocalUserByLoginName(loginName)) {
            return true;
        }

        return false;
    }

}
